﻿@{
    Html.EnableClientValidation();
    Html.EnableUnobtrusiveJavaScript();
}

@Html.DevExpress().GridView(
    settings => {
        settings.Name = "gridView";
        settings.KeyFieldName = "ProductID";
        settings.Width = Unit.Percentage(100);
        settings.CallbackRouteValues = new { Controller = "Editing", Action = "BatchEditingPartial" };
        settings.SettingsEditing.BatchUpdateRouteValues = new { Controller = "Editing", Action = "BatchEditingUpdateModel" };
        
        settings.SettingsEditing.Mode = GridViewEditingMode.Batch;
        settings.SettingsEditing.BatchEditSettings.EditMode = GridViewEditingDemosHelper.BatchEditMode;
        settings.SettingsEditing.BatchEditSettings.StartEditAction = GridViewEditingDemosHelper.BatchStartEditAction;

        settings.CommandColumn.Visible = true;
        settings.CommandColumn.ShowDeleteButton = true;
        settings.CommandColumn.ShowNewButtonInHeader = true;

        settings.Columns.Add(column => {
            column.FieldName = "ProductName";
            column.SetEditItemTemplateContent(c => {
                Html.DevExpress().TextBox(
                    edtSettings => {
                        edtSettings.Name = "ProductNameTextBox";
                        edtSettings.Width = Unit.Percentage(100);
                        edtSettings.Properties.ClientSideEvents.KeyDown = "ProductNameTextBox_KeyDown";
                        edtSettings.Properties.ClientSideEvents.LostFocus = "ProductNameTextBox_LostFocus";
                    }
                ).Render();
            });
        });
        settings.Columns.Add(column => {
            column.FieldName = "CategoryID";
            column.Caption = "Category";

            column.ColumnType = MVCxGridViewColumnType.ComboBox;
            var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
            comboBoxProperties.DataSource = NorthwindDataProvider.GetCategories();
            comboBoxProperties.TextField = "CategoryName";
            comboBoxProperties.ValueField = "CategoryID";
            comboBoxProperties.ValueType = typeof(int);
        });
        settings.Columns.Add("QuantityPerUnit");
        settings.Columns.Add(column => {
            column.FieldName = "UnitPrice";

            column.ColumnType = MVCxGridViewColumnType.SpinEdit;
            var spinEditProperties = column.PropertiesEdit as SpinEditProperties;
            spinEditProperties.DisplayFormatString = "c";
            spinEditProperties.DisplayFormatInEditMode = true;
            spinEditProperties.MinValue = 0;
            spinEditProperties.MaxValue = 1000000;
        });
        settings.Columns.Add(column => {
            column.FieldName = "UnitsInStock";

            column.ColumnType = MVCxGridViewColumnType.SpinEdit;
            var spinEditProperties = column.PropertiesEdit as SpinEditProperties;
            spinEditProperties.NumberType = SpinEditNumberType.Integer;
            spinEditProperties.MinValue = 0;
            spinEditProperties.MaxValue = 10000;
        });
        settings.Columns.Add("Discontinued", MVCxGridViewColumnType.CheckBox);
        settings.ClientSideEvents.BatchEditStartEditing = "gridView_BatchEditStartEditing";
        settings.ClientSideEvents.BatchEditEndEditing = "gridView_BatchEditEndEditing";
        settings.ClientSideEvents.BatchEditRowValidating = "gridView_BatchEditRowValidating";
        settings.CellEditorInitialize = (s, e) => {
            ASPxEdit editor = (ASPxEdit)e.Editor;
            editor.ValidationSettings.Display = Display.Dynamic;
        };
    }).Bind(Model).GetHtml()